(function($){
    $.fetchPaginationDropdown = function (t) {
        var data = {
            action : 'get_pagination_dropdown'
        }
        var send = $.param(data);
        $.ajax({
            url : 'ajax/pages-ajax.php',
            data : send,
            type : 'POST',
            dataType : 'html',
            success : function(data) {
                $(t).html(data);
                $(t).find("select").change(function() {
                    $.fetchPagesTable(".jtable");
                });
            }
        });
    };

    $.fetchPagesTable = function(t) {
        var value;
        if ($("#pages_pagination_dropdown").length == 0) {
            value = 1;
        }
        else {
            value = $('#pages_pagination_dropdown option:selected').attr('value');
        }

        var data = {
            action : 'get_pages_table',
            page : value
        }

        $(".ajax-load").show();

        $(t).jTable({
            url : 'ajax/pages-ajax.php',
            params : data,
            onSuccess : function() {
                $(".ajax-load").hide();
                $(t).trigger("update");
                var sorting = [[1,1],[2,1]];
                $(t).trigger("sorton",[sorting]);
                $(".jtable tbody tr").hoverIntent(
                    function() {
                        $(this).find(".page-control").show();
                    },
                    function() {
                        $(this).find(".page-control").hide();
                });
                $(".page-delete").click(function(){
                    $.submitActionDeletePage(this);
                    return false;
                });
            }
        });
    };

    $.submitActionDeletePage = function(t) {
        var data = {
            action : 'delete_page'
        }

        var send = $.param(data);
        $.ajax({
            url : 'ajax/message-ajax.php',
            data : send,
            type : 'POST',
            dataType : 'html',
            success : function(message) {
                var c = confirm(message);
                if (c) {
                    var value = $(t).attr('rel');
                    data = {
                        action : 'delete_page',
                        nice_url : value
                    }
                    send = $.param(data);
                    $(".ajax-load").show();
                    $.ajax({
                        url : 'ajax/pages-ajax.php',
                        data : send,
                        type : 'POST',
                        dataType : 'html',
                        success : function(data) {
                            $(".ajax-load").hide();
                            $(".ajax-message").htmlTimer(data, 5000);
                            $.fetchPaginationDropdown(".pages-pagination");
                            $.fetchPagesTable(".jtable");
                        }
                    });
                }
            }
        });
    }

})(jQuery);

$(function() {
    $.fetchPaginationDropdown(".pages-pagination");
    $(".jtable").tablesorter();
    $.fetchPagesTable(".jtable");
});